package com.igeek.service.impl;



import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.igeek.mapper.ClaimsMapper;
import com.igeek.mapper.InsuranceFlowable1Mapper;
import com.igeek.model.claims.Claims;
import com.igeek.model.claims.InsuranceFlowable;
import com.igeek.model.claims.InsuranceFlowable1;
import com.igeek.service.IClaimsService;
import com.igeek.service.IInsuranceFlowable1Service;
import com.igeek.service.IInsuranceFlowableService;
import com.igeek.vo.process.ApprovalDto;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-08-20
 */
@Service
public class ClaimsServiceImpl extends ServiceImpl<ClaimsMapper, Claims> implements IClaimsService {
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private ClaimsMapper claimsMapper;
    @Autowired
    private TaskService taskService;
    @Autowired
    private IInsuranceFlowableService insuranceFlowableService;

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private InsuranceFlowable1Mapper insuranceFlowable1Mapper;

    @Override
    public InsuranceFlowable1 startApproval(ApprovalDto approvalDto) {
        Claims claim=new Claims();
        InsuranceFlowable insuranceFlowable=insuranceFlowableService.getById(approvalDto.getId());
        String persons = insuranceFlowable.getPersons();
        String[] person = persons.split(",");
        Map<String,Object> variables=new HashMap<>();
        variables.put("assignee1",person[0]);
        variables.put("assignee2",person[1]);

        //启动流程
        ProcessInstance processInstance=runtimeService.startProcessInstanceById(insuranceFlowable.getProcDefId(),variables);
        //将流程实例id存入表中
        String string = person[0].toString();
        int id=Integer.parseInt(string);

        InsuranceFlowable1 insuranceFlowable1=new InsuranceFlowable1();
        insuranceFlowable1.setId(id);
        insuranceFlowable1.setProcInstId(processInstance.getId());




        claim.setStatus("审核中");
        claim.setAmount(approvalDto.getAmount());
        claim.setItem(approvalDto.getItem());
        claim.setDescription(approvalDto.getDescription());
        claimsMapper.insert(claim);


        String taskId = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult().getId();
        taskService.setAssignee(taskId,person[0]);
        taskService.complete(taskId);//第一个审批完成

        Date startTime = processInstance.getStartTime();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(startTime.toInstant(), zoneId);
        insuranceFlowable1.setStartTime(localDateTime);
        insuranceFlowable1.setClaimsId(claim.getId());
        insuranceFlowable1.setStatus("审核中");
        insuranceFlowable1Mapper.insert(insuranceFlowable1);

        return insuranceFlowable1;
    }
}
